package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Driver;
import com.blb.entity.Logistics;
import com.blb.entity.Vehicle;
import com.blb.mapper.DriverMapper;
import com.blb.mapper.LogisticsMapper;
import com.blb.mapper.VehicleMapper;
import com.blb.service.IVehicleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *物流管理中的车辆管理
 * @author zyx
 * @since 2020-05-21
 */
@Controller
@RequestMapping("/vehicle")
public class VehicleController {

    @Autowired
    private IVehicleService iVehicleService;

    @Resource
    VehicleMapper vehicleMapper;

    @Resource
    LogisticsMapper logisticsMapper;

    @Resource
    DriverMapper driverMapper;

    //车辆列表
    @RequiresPermissions("car")
    @ResponseBody
    @RequestMapping("/v_list")
    public Page<Vehicle> list(int page,int limit){
        return iVehicleService.page(new Page<Vehicle>(page,limit));
    }

    //条件查询
    @RequiresPermissions("car")
    @ResponseBody
    @RequestMapping("/select")
    public Page<Vehicle> select(int page,int limit, String licenseplate){
        return vehicleMapper.selectPage(new Page<Vehicle>(page,limit),new QueryWrapper<Vehicle>().like("t_licenseplate",licenseplate));
    }

    //根据id删除车辆
    @RequiresPermissions("car")
    @ResponseBody
    @RequestMapping("/v_delById")
    public boolean del(String id){
        return iVehicleService.removeById(id);
    }

    //添加操作
    @RequiresPermissions("car:add")
    @ResponseBody
    @RequestMapping("/v_add")
    public boolean add(Vehicle vehicle){
        String id= UUID.randomUUID().toString().replace("-","").substring(0,4);
        vehicle.setId(id);
        vehicle.setTVehicleid(id);
        vehicle.setCreateTime(new Date());
        vehicle.setCompanyId("1");
        return iVehicleService.save(vehicle);
    }

    //修改操作
    @RequiresPermissions("car")
    @ResponseBody
    @RequestMapping("/v_update")
    public boolean update(Vehicle vehicle){
        return iVehicleService.update(vehicle,new UpdateWrapper<Vehicle>().eq("id",vehicle.getId()));
    }

    //跳转添加页面
    @RequiresPermissions("car")
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Logistics> list = logisticsMapper.selectList(new QueryWrapper<Logistics>());
        List<Driver> driverList = driverMapper.selectList(new QueryWrapper<Driver>());
        model.addAttribute("list",list);;
        model.addAttribute("dlist",driverList);
        return "logistics/vehicle/vehicle_add";
    }

    //跳转修改页面
    @RequiresPermissions("car")
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model){
        List<Logistics> list = logisticsMapper.selectList(new QueryWrapper<Logistics>());
        List<Driver> driverList = driverMapper.selectList(new QueryWrapper<Driver>());
        model.addAttribute("list",list);
        model.addAttribute("dlist",driverList);
        return "logistics/vehicle/vehicle_update";
    }


}

